import {LGXEnums}  from '../common/sysenum.js';


import utilfuncs from '../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import { GlobalConstObj } from '../common/globalconstobj.js';
import globalConst from '../common/globalConst.js';
var global = globalConst.GlobalConst;

var CrossModLineSvc = {}

CrossModLineSvc.modifyEnd = function (lineGraph, mouseSelPt,curentPt,selPosIdx) {
    let newSelKind = selPosIdx;
    let position = lineGraph.getPosCopy();
    let posCount = position.posList.length;
    if (posCount == 2) {
        newSelKind = this.modifyTwoPtLine(position,selPosIdx);
    }
    else if (posCount > 2) {
        this.modifyMultiPtLine(position, mouseSelPt,curentPt);
    }

    lineGraph.setPos(position);
    lineGraph.calScope(1);
    return newSelKind;
}

CrossModLineSvc.modifyMultiPtLine = function (position, mouseSelPt,curentPt) {
    let p1 = position.getHead();
    let p2 = position.getTail();
    let next2HeadPt = position.getNext2Head();
    let next2TailPt = position.getNext2Tail();
    let dis1 = funcs.distancePoint(mouseSelPt, p1, 1);
    let dis2 = funcs.distancePoint(mouseSelPt, p2, 1);

    let destNextPt = next2HeadPt;
    let destPt = p1;
    if (dis2 < dis1) {
        destPt = p2;
        destNextPt = next2TailPt;
    }

    let vect = funcs.getVector(destPt,destNextPt);
    let angle = vect.angleToAnotherVector_Degree(GlobalConstObj.xAaxisVector);
    destPt.x = curentPt.x;
    destPt.y = curentPt.y;
    if (Math.abs(angle) < 45){
        //水平处理
        destNextPt.y = curentPt.y;
    }
    else{
        destNextPt.x = curentPt.x;
    }
}

CrossModLineSvc.modifyTwoPtLine = function (position,selPosIdx) {
    let p1 = position.getHead();
    let p2 = position.getTail();

    let newSelKind = selPosIdx;
    let vect = funcs.getVector(p1, p2);
    let angle1 = vect.angleToAnotherVector_Degree(GlobalConstObj.xAaxisVector);
    let angle2 = vect.angleToAnotherVector_Degree(GlobalConstObj.yAaxisVector);
    let hornVertical = 0;
    if (Math.abs(angle1) < global.ZERO_4E) {
        hornVertical = 1;
    }
    else if (Math.abs(angle2) < global.ZERO_4E) {
        hornVertical = 2;
    }
    if (hornVertical == 0) {
        //增加一个拐点
        let newpt1 = { x: 0, y: 0 };
        let newpt2 = { x: 0, y: 0 };
        let vect = funcs.getVector(p1,p2);
        let angle = vect.angleToAnotherVector_Degree(GlobalConstObj.xAaxisVector);
        if (Math.abs(angle) < 45) {
            //扁平的，增加一段垂直线
            newpt1.x = (p1.x + p2.x)/2;
            newpt1.y = p1.y;

            newpt2.x = newpt1.x;
            newpt2.y = p2.y;
        }
        else {
            //纵向偏多，增加一段水平线
            newpt1.x = p1.x;
            newpt1.y = (p1.y + p2.y)/2;

            newpt2.x = p2.x;
            newpt2.y = newpt1.y;
        }

        position.clear();
        position.addPoint(p1);
        position.addPoint(newpt1);
        position.addPoint(newpt2);
        position.addPoint(p2);

        if (selPosIdx == 1){
            newSelKind = 3;
        }
    }
    return newSelKind;
}


export { CrossModLineSvc }